package com.sky.city.interceptor;


import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.common.baselibrary.progress.ResponseConvertFactory;
import com.common.baselibrary.request.ConfigConstant;
import com.common.baselibrary.request.Retrofit2ConverterFactory;
import com.ihsanbal.logging.Level;
import com.ihsanbal.logging.LoggingInterceptor;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.internal.platform.Platform;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;


public class RetrofitHelper {

    public static final int DEFAULT_TIMEOUT = 30;
    private RetrofitService retrofitAzhService;
    private RetrofitService retrofitAzhServiceOther;
    private Retrofit mRetrofit;
    private Retrofit mRetrofitOther;
    private static RetrofitHelper mInstance;

    public static RetrofitHelper getInstance() {
        if (mInstance == null) {
            synchronized (RetrofitHelper.class) {
                mInstance = new RetrofitHelper();
            }
        }
        return mInstance;
    }

    private RetrofitHelper() {

        OkHttpClient.Builder builder = new OkHttpClient.Builder();

        builder.readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .addInterceptor(new LogInterceptor())
                .addInterceptor(new TokenInterceptor())
                .addInterceptor(new LoggingInterceptor.Builder()
                        .setLevel(Level.BASIC)
                        .log(Platform.INFO)
                        .request("Request")
                        .response("Response")
                        .build());
        mRetrofit = new Retrofit.Builder()
                .baseUrl(ConfigConstant.Url.APP_HOST)
//                .baseUrl(LoginUtilsManager.getInstance().getHost())
                .client(builder.build())
                //解析字符串json数据
                .addConverterFactory(new Retrofit2ConverterFactory())
                .addConverterFactory(ResponseConvertFactory.create())
                //添加rxjava支持
                //添加GSON解析：返回数据转换成GSON类型
//                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        mRetrofitOther = new Retrofit.Builder()
                .baseUrl(ConfigConstant.Config.APP_HOST)
//                .baseUrl(LoginUtilsManager.getInstance().getHost())
                .client(builder.build())
                //解析字符串json数据
                .addConverterFactory(new Retrofit2ConverterFactory())
                .addConverterFactory(ResponseConvertFactory.create())
                //添加rxjava支持
                //添加GSON解析：返回数据转换成GSON类型
//                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        retrofitAzhService = mRetrofit.create(RetrofitService.class);

        retrofitAzhServiceOther = mRetrofitOther.create(RetrofitService.class);
    }

    public synchronized static Boolean isNetworkReachable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo current = cm.getActiveNetworkInfo();
        return current != null && (current.isAvailable());
    }


    public Observable login(RequestBody requestBody) {
        Observable<String> stringObservable = toSubscribe(retrofitAzhService.login(requestBody));
        return stringObservable;
    }



    /**
     * rxjava线程调度类
     *
     * @param observable 被观察者
     * @param <T>        类型
     */
    public static <T> Observable toSubscribe(Observable<T> observable) {
        return observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())//开启子线程请求
                .observeOn(AndroidSchedulers.mainThread());//回调在主线程
    }

    /**
     * ConverterJsonToBodyUtils 看自己需求加方法
     */

}
